/**
 * 注册中间件
 */
import { MiddlewareConsumer, RequestMethod } from "@nestjs/common";
import { LoggerMiddleware } from "./middleware/logger.middleware";
import { CatsController } from "./controller/cats.controller";
import { loggerFun } from "./middleware/functionLogger.middleware";
import { proxyMiddleware } from "./middleware/proxy.middleware";
import { MainWebController } from "./controller/IBaseController/mainWeb.controller";
import { PublicController } from "./controller/IBaseController/public.controller";
import { GdbdcWebController } from "./controller/IBaseController/gdbdcWeb.controller";


export function registerMiddleware(consumer: MiddlewareConsumer) {
  /**
   * 使用日志中间件
   * @param consumer
   */
  const uselogMiddleWare = function(consumer: MiddlewareConsumer) {
// 应用 log 中间件
    consumer.apply(LoggerMiddleware)
      // .forRoutes({ path: 'cats', method: RequestMethod.GET });
      // .forRoutes({ path: "*", method: RequestMethod.GET });
      // 也可以链式的 排除之后，再引入一整个controller的连接
      .exclude(
        { path: "cats", method: RequestMethod.GET },
        { path: "cats", method: RequestMethod.POST }
        // "cats/(.*)"
      ).forRoutes(CatsController);
  };
  /**
   * 使用日志中间件
   * @param consumer
   */
  const useLogFunMiddleWare = function(consumer: MiddlewareConsumer) {
    // 若没有任何的依赖，函数式可以直接调用
    consumer.apply(loggerFun).forRoutes("*");
  };
  /**
   * 使用代理中间件
   * @param consumer
   */
  const useProxyMiddleWare = function(consumer: MiddlewareConsumer) {
    /**
     * 注意，如果希望重写或者重新实现对应的接口地址，需要实现Controller
     * 不然reg-path会匹配到字符串并通过中间件来转发
     */
    consumer.apply(proxyMiddleware)
      // 排除加载页面地址 mainWeb/index/*
      .exclude("/mainWeb/index/(.*)")
      .forRoutes(
      "/mainWeb",
      PublicController,
      GdbdcWebController,
      "/workflowWebService",
      "/SouthGISBI",
      "/regulatoryAnalysisWebService",
      "/pubWeb",
      "/proxyWebService",
      "/natureWeb",
      "/logsWeb",
      "/formengineWebService",
      "/editorWebService",
      );
  };

  uselogMiddleWare(consumer);
  useLogFunMiddleWare(consumer);
  useProxyMiddleWare(consumer);
}
